<h1>Proizvodi</h1>

<ul class="left menu sortable">
<? foreach($allGroups as $id=>$name): ?>
    <li class="item" value="<?= $id ?>">
        <span class="handle">x</span>
        <? if($groupId == $id): ?>
            <strong><?= $name ?></strong>
        <? else: ?>
            <?= $this->Html->link($name, '/admin_products/index/' . $id) ?>
        <? endif ?>
    </li>
<? endforeach ?>
<li style="margin-top:5px;border-top:1px dotted #aaa;padding-top:5px">
    <a href="javascript:void(0)" onclick="saveGroup('','')" class="button">+ Nova grupa</a>
</ul>

<? if ($groupId): ?>
    <table class="left entity">
    <caption>
    <?= $this->Html->link('Izmeni', 'javascript:void(0)', array(
        'onclick' => 'saveGroup(\''.$groupId.'\',\''.$groupName.'\')'
    )) ?><?= $this->Html->link('Obriši', '/admin_groups/delete/' . $groupId, array(
        'confirm' => 'Da li želite da obrišete grupu?'
    )) ?><? if($groupHome): ?><?= 
        $this->Html->link('Ukloni sa Home', '/admin_groups/setHome/0/' . $groupId)
    ?><? else: ?><?= 
        $this->Html->link('Postavi na Home', '/admin_groups/setHome/1/' . $groupId) 
    ?><? endif ?>
    | <?= $this->Html->link('Dodaj Proizvod', '/admin_products/create/' . $groupId) ?>
    </caption>
    <tr>
        <th align="left" width="1%">Id</th>
        <th align="left" width="20%">Ime</th>
        <th width="15%">Detalji</th>
        <th width="1%"></th>
    </tr>
    <? if (empty($products)): ?>
    <tr>
        <td colspan="5">Nema proizvoda.</td>
    </tr>
    <? else: ?>
        <? foreach ($products as $product): ?>
        <?php
            $productId = $product['Product']['id'];

            $details = Set::combine($product['ProductDetail'], '{n}.name', '{n}.value');
        ?>
        <tr>
            <td><?= $productId ?></td>
            <td><?= $this->Html->link($product['Product']['name'], '/admin_products/edit/' . $productId) ?></td>
            <td>
                <? foreach ($details as $name=>$value): ?>
                    <? if (preg_match('/^\d+(\.\d+)?$/', $value)) {
                        $value = currency_format($value);
                    } ?>
                    <?= $name ?>: <?= $value?><br>
                <? endforeach ?>
            </td>
            <td>
                <?= $this->Html->link('obriši', '/admin_products/delete/' . $productId, array(
                    'class'   => 'action delete',
                    'confirm' => 'Da li želite da obrišete proizvod?'
                ))?>
            </td>
        </tr>
        <? endforeach; ?>
        <tr class="pagination">
            <td colspan="4"><?= $this->element('pagination') ?></td>
        </tr>
    <? endif ?>
    </table>
<? endif ?>

<script type="text/javascript">
$(function(){
    $('.menu.sortable').sortable({
        revert: 'invalid',
        handle: '.handle',
        placeholder: "placeholder",
        forcePlaceholderSize: true,
        items: '.item',
        stop: function() {
            var positions = {}
            var ul = $('.menu.sortable');
            ul.children('li').each(function(index){
                positions[this.value] = index;
            });

            $.ajax({
                url: '/admin_groups/update_positions',
                data: {positions: positions},
                beforeSend: function() {
                    ul.addClass('loading')
                },
                complete: function(req) {
                	ul.removeClass('loading');
                	if (req.status != 200) {
                    	alert(req.responseText)
                	}
                }
            });
        }
    });
});

function saveGroup(id, name) {
	var groupName=prompt('Naziv grupe:', name);
	if (groupName) {
		$.ajax({
			url:  '/admin_groups/save',
			type: 'post',
			data: {
			    data: {
				    id: id, 
				    name: groupName,
				    home: <?= $groupHome ?>
			    }
		    },
			beforeSend: function() {
				$('ul.menu.sortable').addClass('loading');
			},
			complete: function(req) {
				$('ul.menu.sortable').removeClass('loading');
				if (req.status==200) {
					location.href = '/admin_products/index/' + req.responseText
				} else {
					alert(req.responseText);
				}
			}
		});
	}
}
</script>